package lc.com.lcpicturebackend.manager.app.rag;

import org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.filter.Filter;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;
import org.springframework.stereotype.Component;

/**
 * 功能：使用 Spring 内置的文档检索器提供的 filterExpression 配置过滤规则
 * 作者：lc
 * 日期：2025/6/4 22:28
 */
@Component
public class AppRagCustomAdvisorFactory {
    public static Advisor create(VectorStore vectorStore, String status) {
        // 配置过滤规则
        Filter.Expression expression = new FilterExpressionBuilder()
                .eq("type", status)
                .build();

        // 创建自定义检索增强器
        VectorStoreDocumentRetriever documentRetriever = VectorStoreDocumentRetriever.builder()
                .vectorStore(vectorStore)
                .filterExpression(expression) // 设置过滤规则
                .similarityThreshold(0.4)
                .topK(4) // 返回文档数量
                .build();
        // 返回自定义过滤器
        return RetrievalAugmentationAdvisor.builder()
                .documentRetriever(documentRetriever)
                .queryAugmenter(AppContextualQueryAugmenter.create())
                .build();


    }
}
